﻿using CatUtils.Utils.TypeConvert;
using System;
using System.Net;
using static CatUtils.Utils.Currency.PrintLog;

namespace CatUtilsDll.Utils.Currency.WebUtils
{/// <summary>
 /// 网络请求帮助
 /// </summary>
    public partial class WebHelper
    {
        #region 同步上传方法

        /// <summary>
        /// 上传数据
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <param name="UploadByte"></param>
        /// <returns></returns>
        public string UploadData(string RequestUrl, byte[] UploadByte)
        {
            return UploadData(RequestUrl, UploadByte, WebHelper.PcWebHeader());
        }

        /// <summary>
        /// 上传数据
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <param name="UploadByte"></param>
        /// <param name="webHeaderCollection"></param>
        /// <returns></returns>
        public string UploadData(string RequestUrl, byte[] UploadByte, WebHeaderCollection webHeaderCollection)
        {
            string Shtml = "";
            try
            {
                using (WebClient webClient = new WebClient())
                {
                    webClient.Headers = webHeaderCollection;

                    byte[] vs = webClient.UploadData(RequestUrl, UploadByte);
                    Shtml = new ByteConvert().ByteToStr(vs);
                }
            }
            catch (Exception ex)
            {
                Log(ex);
            }

            return Shtml;
        }

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public static string UploadFile(string RequestUrl, string FilePath)
        {
            return UploadFile(RequestUrl, FilePath, PcWebHeader());
        }

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <param name="FilePath"></param>
        /// <param name="webHeaderCollection"></param>
        /// <returns></returns>
        public static string UploadFile(string RequestUrl, string FilePath, WebHeaderCollection webHeaderCollection)
        {
            string Shtml = "";
            try
            {
                using (WebClient webClient = new WebClient())
                {
                    webClient.Headers = webHeaderCollection;

                    byte[] vs = webClient.UploadFile(RequestUrl, FilePath);
                    Shtml = new ByteConvert().ByteToStr(vs);
                }
            }
            catch (Exception ex)
            {
                Log(ex);
            }

            return Shtml;
        }

        #endregion 同步上传方法
    }
}